package com.biostime.springdraw.modules.sd.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.springdraw.common.config.Global;
import com.biostime.springdraw.common.persistence.Page;
import com.biostime.springdraw.common.utils.StringUtils;
import com.biostime.springdraw.common.web.BaseController;
import com.biostime.springdraw.modules.sd.entity.Member;
import com.biostime.springdraw.modules.sd.service.MemberService;
import com.biostime.springdraw.modules.sys.entity.User;

/**
 * 微信用户Controller
 * @author king
 * @version 2016-01-18
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/member")
public class MemberController extends BaseController {

	@Autowired
	private MemberService MemberService;
	
	@ModelAttribute
	public Member get(@RequestParam(required=false) String id) {
		Member entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = MemberService.get(id);
		}
		if (entity == null){
			entity = new Member();
		}
		return entity;
	}
	
	@RequiresPermissions("sd:member:view")
	@RequestMapping(value = {"list", ""})
	public String list(Member Member, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Member> page = MemberService.findPage(new Page<Member>(request, response), Member); 
		model.addAttribute("page", page);
		return "modules/sd/MemberList";
	}

	@RequiresPermissions("sd:member:view")
	@RequestMapping(value = "form")
	public String form(Member Member, Model model) {
		model.addAttribute("Member", Member);
		return "modules/sd/MemberForm";
	}
	
	@RequiresPermissions("sd:member:edit")
	@RequestMapping(value = "save")
	public String save(Member Member, Model model, RedirectAttributes redirectAttributes) {
		Member.setCreateBy(new User("0"));
		Member.setCreateDate(new Date());
		if (!beanValidator(model, Member)){
			return form(Member, model);
		}
		MemberService.save(Member);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/sd/member/?repage";
	}
	
	@RequiresPermissions("sd:member:delete")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Member Member, RedirectAttributes redirectAttributes) {
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				MemberService.delete(new Member(id));
			}
		}else{
			MemberService.delete(Member);
		}	
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/sd/member/?repage";
	}

}